package com.wangxiang.sys.manage.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.sys.manage.dao.AuditDao;
import com.wangxiang.sys.manage.dao.RoleDao;
import com.wangxiang.sys.manage.dao.RoleUserDao;
import com.wangxiang.sys.manage.dao.UserDao;
import com.wangxiang.sys.manage.entity.RoleEntity;
import com.wangxiang.sys.manage.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.wangxiang.sys.manage.entity.AuditEntity;
import com.wangxiang.sys.manage.service.AuditService;
import org.springframework.web.bind.annotation.RequestBody;


@Service("auditService")
public class AuditServiceImpl extends ServiceImpl<AuditDao, AuditEntity> implements AuditService {
    @Autowired
    private AuditDao auditDao;
    @Autowired
    private RoleUserDao roleUserDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserDao userDao;

    @Override
    public int add(HashMap<String, Integer> map) {
        AuditEntity auditEntity = new AuditEntity();
        auditEntity.setSpuId(map.get("spuId"));
        auditEntity.setUserId(map.get("userId"));
        auditEntity.setTime(new Date());
        auditEntity.setStatus(map.get("status")); // 0恢复上架申请 1新增申请 2已审核（日志表存审核结果 0 审核通过 1 违规商品）
        //添加
        int insert = auditDao.insert(auditEntity);

        return insert;
    }

    @Override
    public PageEntity page(HashMap<String , Integer> map) {
        PageHelper.startPage(map.get("pageNum"),map.get("pageSize"));
        List<AuditEntity> auditEntities = auditDao.selectList(null);
//        System.out.println("数据库"+auditEntities);
        //申请时间需要转换
        TimeUtils timeUtils = new TimeUtils();
        auditEntities.forEach((item)->{
            String time = timeUtils.timeChange(item.getTime());
            item.setStrTime(time);

        });
        PageInfo<AuditEntity> auditEntityPageInfo = new PageInfo<>(auditEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(auditEntityPageInfo);
        return generate;
    }

    @Override
    public List<HashMap<String, Object>> pageList(List<AuditEntity> list) {
        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        for (AuditEntity auditEntity : list) {
            HashMap<String, Object> map = new HashMap<>();
            //申请人名称，状态，申请人角色，商品状态，申请时间，工单号(主键id)，spuid,applyUserId
            UserEntity entity = userDao.selectById(auditEntity.getUserId());
            int roleId = roleUserDao.selectRoleId(auditEntity.getUserId());
            RoleEntity roleEntity = roleDao.selectById(roleId);
            map.put("applyName",entity.getLoginId());
            map.put("applyRole",roleEntity.getName());
            map.put("applyUserId",auditEntity.getUserId());
            map.put("applyRoleId",roleId);

            map.put("DBTime",auditEntity.getTime());
            map.put("time",auditEntity.getStrTime());
            map.put("status",auditEntity.getStatus());
            String spuId = String.valueOf(auditEntity.getSpuId());
            map.put("spuId",spuId);
            String s = String.valueOf(auditEntity.getId());
            map.put("orderId",s);
            hashMaps.add(map);
        }
        return hashMaps;
    }



}